package com.hqd.jjvm.instructions.stack;

import com.hqd.jjvm.instructions.AbstractInstruction;
import com.hqd.jjvm.instructions.InstructionType;
import com.hqd.jjvm.rtda.JThread;
import com.hqd.jjvm.rtda.slot.Slot;

public class StackInstruction extends AbstractInstruction {

    public StackInstruction(JThread jThread) {
        super(jThread);
    }

    @Override
    public void execute(InstructionType instructionType) {
        switch (instructionType) {
            case POP: {
                popSlot();
                break;
            }
            case POP2: {
                popSlot();
                popSlot();
                break;
            }
            case DUP: {
                Slot s1 = popSlot();
                pushSlot(s1);
                pushSlot(s1);
                break;
            }
            case DUP2: {
                Slot s1 = popSlot();
                Slot s2 = popSlot();
                pushSlot(s2);
                pushSlot(s1);
                pushSlot(s2);
                pushSlot(s1);
                break;
            }
            case DUP_X1: {
                Slot s1 = popSlot();
                Slot s2 = popSlot();
                pushSlot(s1);
                pushSlot(s2);
                pushSlot(s1);
                break;
            }
            case DUP_X2: {
                Slot s1 = popSlot();
                Slot s2 = popSlot();
                Slot s3 = popSlot();
                pushSlot(s1);
                pushSlot(s3);
                pushSlot(s2);
                pushSlot(s1);
                break;
            }
        }
    }
}
